<template>
  <div class="page">
    <v-pageTitle title="部门管理"></v-pageTitle>
    <v-pageSearch>
      <el-form :inline="true">
         <el-form-item>
           <el-button  type="primary" @click="batchAdd" size="small">新增部门</el-button>
         </el-form-item>
      </el-form>
    </v-pageSearch>
    <!--工具条-->
    <!--<v-pageToolbar>
        <el-button  type="danger" size="small" @click="batchClick" :disabled="this.sels.length===0">批量删除</el-button>
    </v-pageToolbar>-->
    <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="center">
      <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%" @selection-change="selsChange" :default-sort="{prop: 'date', order: 'descending'}">
          <el-table-column fixed type="selection" width="50">
          </el-table-column>
          <el-table-column type="index" label="序号" width="120" align="center">
          </el-table-column>
          <el-table-column prop="post" label="岗位名称" min-width="120" sortable>
          </el-table-column>
          <el-table-column prop="name" label="所属部门" min-width="120" sortable>
          </el-table-column>
          <el-table-column label="操作" min-width="150">
              <template slot-scope="scope">
                <el-button  @click="batchEdit(scope.row)" type="primary" size="small">编辑</el-button>
                <el-button  @click="handleDel(scope.$index, scope.row)" type="danger" size="small">删除</el-button>
              </template>
          </el-table-column>
      </el-table>
    </v-pageTable>

    <!--编辑界面-->
    <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称" prop="post">
          <el-input v-model="ruleForm.post"></el-input>
        </el-form-item>
        <el-form-item label="当前时间">
            <h3 style="font-weight:400;">{{nowDate}}</h3>
        </el-form-item>
        <el-form-item>
            <el-button  type="primary" @click.native="submitForm('ruleForm')" :loading="editLoading">提交</el-button>
            <el-button  @click.native="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import util from '../../../util/util'
export default {
    data() {
      return {
        tableData: {
            loading: true,
            head: [],
            body: []
        },
        paginationTotal: 100,
        sels:[],

        //编辑界面数据
		    ruleForm: {
          userName: '',
          name: '',
          gender: 0,
          tel: '',
          email: '',
          branch: '',
          post: '',
          status: false,
          mark: ''
        },
        branch_options: [
          {
            value: '新业务部',
            label: '新业务部'
          }, {
            value: '选项2',
            label: '双皮奶'
          }, {
            value: '选项3',
            label: '蚵仔煎'
          }, {
            value: '选项4',
            label: '龙须面'
          }, {
            value: '选项5',
            label: '北京烤鸭'
          }],
          post_options: [{
            value: '前端开发工程师',
            label: '前端开发工程师'
          }, {
            value: '选项2',
            label: '双皮奶'
          }, {
            value: '选项3',
            label: '蚵仔煎'
          }, {
            value: '选项4',
            label: '龙须面'
          }, {
            value: '选项5',
            label: '北京烤鸭'
          }
        ],
        rules: {
          name: [
            { required: true, message: '请输入部门名称', trigger: 'blur' }
          ],
          post: [
            { required: true, message: '请输入岗位名称', trigger: 'blur' }
          ]
        },
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,

        nowDate: util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    filters: {
      statusFilter(statue) {
        let sta = statue ? 0 : 1;
        let statusMap = ['禁用','启用']
        return statusMap[sta]
      },
      statusFilterType(statue) {
        let sta = statue ? 0 : 1;
        let statusMapType = ['success','error']
        return statusMapType[sta]
      },
      genderFilter(statue) {
        let statusMap = ['未知','男','女']
        return statusMap[statue]
      },
      genderFilterType(statue) {
        let statusMapType = ['primary','error','success']
        return statusMapType[statue]
      }
    },
    methods: {
        getTableData(data) {
            // ajax 查询表格数据
            this.tableData.body = [
                {
                    name: '新业务部',
                    userName: 'Jesen',
                    gender: 1,
                    tel: '13724568432',
                    email: '1234534@qq.com',
                    passWord: '123456',
                    branch: '新业务部',
                    post: '前端开发工程师',
                    status: false
                }, {
                    name: '软件部',
                    userName: 'Jesen',
                    gender: 1,
                    tel: '13724568432',
                    email: '1234534@qq.com',
                    passWord: '123456',
                    branch: '新业务部',
                    post: '安卓开发工程师',
                    status: false
                }, {
                    name: '市场部',
                    userName: 'Jesen',
                    gender: 1,
                    tel: '13724568432',
                    email: '1234534@qq.com',
                    passWord: '123456',
                    branch: '新业务部',
                    post: '业务员',
                    status: false
                }
            ]
            // 模拟加载
            setTimeout(() => {
                this.tableData.loading = false
            }, 1000)
        },
        sizeChange(val){
            console.log(val)
            this.getTableData()
        },
        pageChange(val){
            console.log(val)
            this.getTableData()
        },
        selsChange: function (sels) {
            this.sels = sels;
        },
        handleClick(row) {
            console.log(row);
        },
        //显示新增界面
        batchAdd(){
            this.editFormVisible = true;
        },
        //显示编辑界面
        batchEdit: function (row) {
            this.editFormVisible = true;
            this.ruleForm = Object.assign({}, row);
        },
        batchClick(){
            // var ids = this.sels.map(item => item.id).toString();
            console.log(this.sels)
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                alert('submit!');
                this.editFormVisible = false;
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        //删除
        handleDel: function (index, row) {
            this.$confirm('确认删除该岗位吗?', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                //NProgress.start();
                let para = { id: row.id };
                removeUser(para).then((res) => {
                    this.listLoading = false;
                    //NProgress.done();
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                    this.getUsers();
                });
            }).catch(() => {

            });
        }
    },
    watch: {
      //第一种写法 适用于普通变量（简单类型的值的观测写法）
      // 关闭dialog的时候 初始化页面表单数据
      editFormVisible: function (val, oldVal) {
        console.log('new: %s, old: %s', val, oldVal)
        if(!val){
          console.log(this.ruleForm)
          this.ruleForm = {
            userName: '',
            name: '',
            gender: 0,
            tel: '',
            email: '',
            branch: '',
            post: '',
            status: false,
            mark: ''
          }
        }
      }
    },
    mounted() {
        // 获取table数据
        this.getTableData()

        let  _this = this;
        this.timer = setInterval(function(){
            _this.nowDate = util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss');
        },1000);
    },
    beforeDestory(){
        if(this.timer){
            clearInterval(this.timer)
        }
    }
  }
</script>